/**
 * Interest.java
 *
 * @created Mardi 06/03/2007
 * 
 * @author bbrison
 * @author jprieur
 */

package communication.pack;

import kernel.*;
import objects.*;
import java.awt.geom.*;

public class Interest extends SMPackage {

    private double _startTime;

    /**
     * The destination, the area where the measure will be made
     */
    private Rectangle2D.Double _destinationField;

    /**
     * The distance between the current location of the interest and the destination field
     */
    private double _distance;

    /**
     * The constructor of an Interest
     * @param destination the destination, meaning what it has to be measured
     * @param x the point, the station, on the X axis, where the interest is constructed 
     * @param y the point, the station, on the Y axis, where the interest is constructed
     */ 
    public Interest(Station sender, 
		    int sequencenb, 
		    int hopcount, 
		    int requestnb, 
		    int ttl, 
		    Rectangle2D.Double destination) {
	super(sender,sequencenb,hopcount,requestnb,INTEREST,ttl);
	_startTime = 0;
	_destinationField = destination;
	double xx = Math.pow(sender.getX()-_destinationField.getCenterX(),2);
	double yy = Math.pow(sender.getY()-_destinationField.getCenterY(),2);
	_distance = Math.sqrt(xx+yy);

    }

    public Interest(Interest i){
	super(i);
	_startTime = i.getStartTime();
	_destinationField = i.getDestinationField();
	_distance = i.getDistance();
    }

    public Rectangle2D.Double getDestinationField(){
	return _destinationField;
    }

    public double getDistance(){
	return _distance;
    }
    
    public double getStartTime()
    {
	return _startTime;
    }
    
    public void setDistance(double distance) {
	_distance = distance;
    }
    
    public void setProperties(DataType data)
    {
	DataInterest di = (DataInterest)data;
	senderId = di.getSink();
	requestId = di.getId();
	_startTime = di.getStartTime();
	_destinationField = new Rectangle2D.Double(di.getCoordX(),
						   di.getCoordY(),
						   di.getWidth(),
						   di.getHeight());
	double xx = Math.pow(senderId.getX()-_destinationField.getCenterX(),2);
	double yy = Math.pow(senderId.getY()-_destinationField.getCenterY(),2);
	_distance = Math.sqrt(xx+yy);
	
    }

    public DataType getProperties()
    {
	return new DataInterest((Sink)senderId,
				requestId,
				_startTime,
				_destinationField.getX(),
				_destinationField.getY(),
				_destinationField.getWidth(),
				_destinationField.getHeight());
    }

    public String toString() {
	String info = "INTEREST : \n" + super.infoPackage();
	info += "\n" + "coordinates of the destination field : (" +
	    _destinationField.getX() + ";" +
	    _destinationField.getY() + ")";
	info += "\n" + "distance from destination : " + _distance;
	return info;
    }
}
